from django.contrib import admin
from django.utils.html import format_html

from project_manage.apps.project.models import ProjectModel, ProjectDefectPostModel


# Register your models here.


# @admin.register(ProjectModel)
# class ProjectModelAdmin(admin.ModelAdmin):
#     list_display = ('name', 'get_description', 'start_date', 'end_date')
#     fieldsets = (
#         ('基本信息', {'fields': ('name', 'description', ('start_date', 'end_date',),)}),
#         ('人员', {'fields': ('testers', 'developer')}),
#     )
#     filter_horizontal = ('testers', 'developer',)
#
#     def get_queryset(self, request):
#         user = request.user
#         role = user.role
#         if role == 10:
#             # 开发者
#             return user.dev_project.all()
#         elif role == 20:
#             # 测试员
#             return user.test_project.all()
#         else:
#             # 管理员
#             return super().get_queryset(request)
#
#     def get_description(self, obj):
#         if obj.description:
#             if len(obj.description) > 12:
#                 html = """<span @click="showDescription('{0}')">{1}...</span>""".format(obj.description,
#                                                                                         obj.description[:12])
#                 return format_html(html)
#             return obj.description
#         else:
#             return '无'
#
#     get_description.short_description = '项目描述'


# @admin.register(ProjectDefectPostModel)
# class ProjectDefectPostModelAdmin(admin.ModelAdmin):
#     list_display = ('title', 'get_description', 'project', 'defect_level', 'release_time',
#                     'response_time', 'get_before_defect_image', 'get_after_defect_image', 'status', 'get_option')
#     fieldsets = (
#         ('基本信息', {'fields': ('title', 'description',
#                              ('project', 'defect_level'),
#                              ('release_time', 'expected_resolution_time'),
#                              ('before_defect_image',),
#                              )
#                   }
#          ),
#         ('状态', {'fields': ('status',)}),
#     )
#     readonly_fields = ('status',)
#     list_filter = ('defect_level', 'release_time', 'status')
#
#     def save_model(self, request, obj, form, change):
#         obj.creator = request.user
#         return super().save_model(request, obj, form, change)
#
#     def get_queryset(self, request):
#         self.request = request
#         user = request.user
#         role = user.role
#         if role == 10:
#             # 开发者
#             return ProjectDefectPostModel.objects.filter(project__in=user.dev_project.all())
#         elif role == 20:
#             # 测试员
#             return ProjectDefectPostModel.objects.filter(project__in=user.test_project.all())
#         else:
#             # 管理员
#             return super().get_queryset(request)
#
#     def get_description(self, obj):
#         if obj.description:
#             if len(obj.description) > 12:
#                 html = """<span @click="showDescription('{0}')">{1}...</span>""".format(obj.description,
#                                                                                         obj.description[:12])
#                 return format_html(html)
#             return obj.description
#         else:
#             return '无'
#
#     get_description.short_description = '缺陷描述'
#
#     def get_before_defect_image(self, obj):
#         html = "无缺陷图"
#         if obj.before_defect_image:
#             html = """<img src="{0}" style="width: 50px;height: 50px" @click="showImg('{0}')">""".format(
#                 obj.before_defect_image)
#         return format_html(html)
#
#     get_before_defect_image.short_description = '缺陷图'
#
#     def get_after_defect_image(self, obj):
#         html = "无修复图"
#         if obj.after_defect_image:
#             html = """<img src="{0}" style="width: 50px;height: 50px" @click="showImg('{0}')">""".format(
#                 obj.after_defect_image)
#         return format_html(html)
#
#     get_after_defect_image.short_description = '修复图'
#
#     def get_option(self, obj):
#         if obj.status == 0:
#             html = """
#                     <div>
#                     <el-button type="danger" plain size="mini" disabled>已关闭</el-button>
#                     </div>
#                     """.format(id=obj.pk)
#         elif obj.status == 1:
#             if self.request.user.role == 10:
#                 # 开发者
#                 html = """
#                         <el-button type="success" plain size="mini" @click="responseReport('{id}')">立即响应</el-button>
#                         """.format(id=obj.pk)
#             elif self.request.user.role == 20:
#                 # 测试员
#                 html = """
#                         <el-button type="danger" plain size="mini" @click="closeReport('{id}')">关闭报告</el-button>
#                         """.format(id=obj.pk)
#             else:
#                 # 管理员
#                 html = """
#                         <div>
#                         <el-button type="success" plain size="mini" @click="responseReport('{id}')">立即响应</el-button>
#                         </div>
#                         <div style="margin-top:5px;">
#                         <el-button type="danger" plain size="mini" @click="closeReport('{id}')">关闭报告</el-button>
#                         </div>
#                 """.format(id=obj.pk)
#         elif obj.status == 2:
#             html = """
#                     <div>
#                     <input type="file" ref="filElem" hidden @change="completeReport('{id}')" />
#                     <el-button type="success" size="mini" @click="choiceImg" {disabled}>确认修复</el-button>
#                     </div>
#             """.format(id=obj.pk, disabled='' if self.request.user.role == 10 else 'disabled')
#         else:
#             html = """
#                     <div>
#                     <el-button type="success" plain size="mini" disabled>修复完成</el-button>
#                     </div>
#                     """.format(id=obj.pk)
#         return format_html(html)
#
#     get_option.short_description = '操作'
